package com.ruoyi.app.common

import cn.hutool.core.io.FileUtil
import cn.hutool.core.util.RandomUtil
import cn.hutool.core.util.URLUtil
import com.alibaba.fastjson.JSON
import com.ruoyi.back.domain.v3.VideoSinglePriceDto
import io.swagger.annotations.ApiModelProperty
import ws.schild.jave.MultimediaObject
import java.math.BigDecimal
import java.math.RoundingMode
import java.net.URL


fun String.time(): Long {
    val randomFileName = RandomUtil.randomNumbers(6)
    return FileUtil.newFile("/home/$randomFileName .mp4")
        .let { file ->
            try {
                FileUtil.writeFromStream(URLUtil.getStream(URL(this)), file)

                BigDecimal(
                    MultimediaObject(file)
                        .info.duration
                )
                    .divide(
                        BigDecimal(1000),
                        0,
                        RoundingMode.HALF_UP
                    )
                    .longValueExact()
            }finally {
                FileUtil.del(file)
            }
        }
}

fun durationDesc(duration: Long) = "${String.format("%02d", duration/60)}:${String.format("%02d", duration%60)}"

fun main() {

    println(JSON.toJSON(listOf(
        VideoSinglePriceDto().apply {
            minSecond = 1
            maxSecond = 10
            price = 11.0
        },

        VideoSinglePriceDto().apply {
            minSecond = 11
            maxSecond = 20
            price = 13.0
        },

        VideoSinglePriceDto().apply {
            minSecond = 21
            maxSecond = 50
            price = 16.0
        },

        VideoSinglePriceDto().apply {
            minSecond = 51
            addEverySecond = 3
            price = 2.0
        }
    )))

    println(durationDesc(3610))
}